package com.webBasic.Util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;

public class GyUtil {
    public static Object invokeGetterMethod(Object object, String getterMethodName) {
        getterMethodName = "get" + getGetterMethodName(getterMethodName);

        try {
            Object o=object.getClass().getMethod(getterMethodName).invoke(object);
            return o;
        } catch (Exception var2) {
            return null;
        }
    }

    public static void ajaxPageResult(JSONObject root, HttpServletResponse response) {
        try {
            response.setContentType("text/json;charset=UTF-8");
            response.setDateHeader("Expires", 1L);
            response.addHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache, no-store, max-age=0");
            root.put("status", "SUCCESS");
            ObjectMapper mapper=new ObjectMapper();
            mapper.writeValue(response.getWriter(), root);
        } catch (Exception var5) {
            //this.logger.error(var5.getMessage(), var5);
        }
    }

    /**
     * 根据属性名称和java类型，获取对应的getter方法名
     * @param property
     * @return
     */
    public static String getGetterMethodName(String property) {
        StringBuilder sb = new StringBuilder();
        sb.append(property);
        if (Character.isLowerCase(sb.charAt(0))) {
            if (sb.length() == 1 || !Character.isUpperCase(sb.charAt(1))) {
                sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
            }
        }
        return sb.toString();
    }


}
